"use strict";

function getSign(address) {
  var timestamp = new Date() * 1;
  var source = "web";
  // var random = Math.random();
  var token = "";
  
  if (!getCookie('signature')) {
    token = "";
  } else {
    token = getCookie('signature');
  }
  // var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJhZG1pbkZsYWdcIjoxLFwiY29tcGFueUlkXCI6MTAwMDE0MSxcImNvbXBhbnlOYW1lXCI6XCI2MTDotYTkuqdcIixcImNvbXBhbnlUeXBlXCI6MixcImlkXCI6MjcsXCJuYW1lXCI6XCLlsI_mmI5cIixcInJvbGVDb2RlXCI6XCJST0xFX0FTU0VUXCJ9IiwiZXhwIjoxNTYwNDM4Mjk3fQ.T35FuNmdRQfsCVB2aW3LnaSs9cgLg2XCCZAxiK5f2I4";
  var index = address.indexOf("/api");
  var url = address.slice(index);
  var sign = hex_md5(timestamp + source + token + url);

  var headers = {
    timestamp: timestamp,
    source: source,
    token: token,
    sign: sign,
    "Content-Type": "application/json"
  };
  
  return headers;
}
function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(";");
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  }
  return "";
}

var service = new Object();
service.host_url = "";
service.request = function (address, method, options, back) {
  var headers = getSign(address);
  // var headers = {debug:'1'};
  if (method == "get") {
    axios.get(service.host_url + address, {
      params: options,
      headers: headers
    }).then(function (res) {
      if (res.data.code == 407) {
        window.location.href = "/console/#/Login";
      } else {
        back(res);
      }
    });
  } else if (method == "post") {
    axios.post(service.host_url + address, options, {
      headers: headers
    }).then(function (res) {
      if (res.data.code == 407) {
        window.location.href = "/console/#/Login";
      } else {
        back(res);
      }
    });
  }
};

// 登录
var login_server = new Object();
// login_server.login = function(params, back) {
//   service.request(
//     "/videosurvey/scheduleCase/getAllSeat",
//     "get",
//     params,
//     function(data) {
//       back(data);
//     }
//   );
// };

/*登陆注册********************************************** */
//登录
login_server.login = function (data, back) {
  service.request("/zhixuan/api/v1/user/login", "post", data, function (data) {
    back(data);
  });
};
//注册
login_server.register = function (data, back) {
  service.request("/zhixuan/api/v1/user/register", "post", data, function (data) {
    back(data);
  });
};
//获取验证码
login_server.getVerifyCode = function (data, back) {
  service.request("/zhixuan/api/v1/user/getVerifyCode", "post", data, function (data) {
    back(data);
  });
};
//忘记密码
login_server.forgetPassword = function (data, back) {
  service.request("/zhixuan/api/v1/user/forgetPassword", "post", data, function (data) {
    back(data);
  });
};

// 房源
var house_server = new Object();
/*
 **楼盘接口
 */
// 4.01获取楼盘图片列表
house_server.getBuildImg = function (query, back) {
  service.request("/zhixuan/api/v1/building/getBuildingOutDoorPic", "post", query, function (data) {
    back(data.data);
  });
};
// 获取全部待租空间
house_server.getAllWait = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/attention/allwaithouse/json", "post", query, function (data) {
    back(data.data);
  });
};
// 获取楼盘基本信息
house_server.getHouseInfo = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/house/gethouseinfo", "post", query, function (data) {
    back(data.data);
  });
};
// 获取楼盘周边推荐
house_server.getRecommend = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/house/recommend", "post", query, function (data) {
    back(data.data);
  });
};

/**公共接口******************************************************************************************/
//获取浏览量量
house_server.getCountVisit = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/countVisit", "post", query, function (data) {
    back(data.data);
  });
};
//获取浏览量量(多)
house_server.getCountVisits = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/countVisits", "post", query, function (data) {
    back(data.data);
  });
};
//添加浏览记录
house_server.addVisit = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/addVisit", "post", query, function (data) {
    back(data.data);
  });
};
//获取关注人数
house_server.getAttentionTypeNum = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/attentionTypeNum", "post", query, function (data) {
    back(data.data);
  });
};
//获取关注人数(多
house_server.getAttentionTypeNums = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/attentionTypeNums", "post", query, function (data) {
    back(data.data);
  });
};
// 获取收藏人数
house_server.getHouseCollectionNum = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/houseCollectionNum", "post", query, function (data) {
    back(data.data);
  });
};
//获取收藏人数(多)
house_server.getHouseCollectionNums = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/houseCollectionNums", "post", query, function (data) {
    back(data.data);
  });
};
//获取咨询次数
house_server.getPhoneStatsNum = function (query, back) {
  service.request("/zhixuan/api/v1/consult/stats", "post", query, function (data) {
    back(data.data);
  });
};
//点击电话记录咨询次数
house_server.addPhoneNum = function (query, back) {
  service.request("/zhixuan/api/v1/consult/append", "post", query, function (data) {
    back(data.data);
  });
};
//获取房源/楼盘/公司 均价
house_server.getStatsBuiHouseAvgPrice = function (query, back) {
  service.request("/zhixuan/api/v1/building/statsBuiHouseAvgPrice", "post", query, function (data) {
    back(data.data);
  });
};
//获取品牌/社区 均价
house_server.getStatsCommunityAvgPrice = function (query, back) {
  service.request("/zhixuan/api/v1/community/statsCommunityAvgPrice", "post", query, function (data) {
    back(data.data);
  });
};
//2.10 获取楼书用户信息
house_server.brochureUserInfo = function (query, back) {
  service.request("/zhixuan/api/v1/brochure/brochureUserInfo", "post", query, function (data) {
    back(data.data);
  });
};
//获取品牌/社区 均价
house_server.getHotTradingArea = function (query, back) {
  service.request("/zhixuan/api/v1/filterOrSort/hotTradingArea", "get", query, function (data) {
    back(data.data);
  });
};
/********************************************************************************************/

//经纪人/管理公司 区县房源数量
house_server.getStatTownHouseNum = function (query, back) {
  service.request("/zhixuan/api/v1/building/statTownHouseNum", "post", query, function (data) {
    back(data.data);
  });
};

//根据城市id获取区县
house_server.getCommonCityItems = function (params, back) {
  service.request("/zhixuan/api/v1/common/getCommonCityItems", "get", params, function (data) {
    back(data.data);
  });
};
//获取热门楼盘
house_server.getHotBuilding = function (params, back) {
  service.request("/zhixuan/api/v1/filterOrSort/hotBuilding", "get", params, function (data) {
    back(data.data);
  });
};
//获取热门品牌
house_server.hotBrand = function (params, back) {
  service.request("/zhixuan/api/v1/filterOrSort/hotBrand", "get", params, function (data) {
    back(data.data);
  });
};
//获取热搜便签
house_server.getHotSearch = function (query, back) {
  service.request("/zhixuan/api/v1/filterOrSort/hotSearch", "post", query, function (data) {
    back(data.data);
  });
};
//获取热搜便签
house_server.getAllRecommendHouse = function (query, back) {
  service.request("/zhixuan/api/v1/company/apiAllRecommendHouse", "get", query, function (data) {
    back(data.data);
  });
};
//写字楼园区按楼盘搜索
house_server.searchBuildingPage = function (query, back) {
  service.request("/zhixuan/api/v1/building/searchBuildingPage", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公按社区搜索
house_server.searchCommunityPage = function (query, back) {
  service.request("/zhixuan/api/v1/community/searchCommunityPage", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公搜索-按品牌
house_server.searchByBrandPage = function (query, back) {
  service.request("/zhixuan/api/v1/community/searchBrandPage", "post", query, function (data) {
    back(data.data);
  });
};
//资产管理公司搜索
house_server.searchByHousePage = function (query, back) {
  service.request("/zhixuan/api/v1/building/searchBuiHouseByAssetPage", "post", query, function (data) {
    back(data.data);
  });
};
//推荐房源
house_server.recommendHouse = function (params, back) {
  service.request("/zhixuan/api/v1/filterOrSort/recommendHouse", "get", params, function (data) {
    back(data.data);
  });
};
//推荐楼盘
house_server.recommendBuilding = function (params, back) {
  service.request("/zhixuan/api/v1/filterOrSort/recommendBuilding", "get", params, function (data) {
    back(data.data);
  });
};
//房源周边推荐
house_server.recommendedHouseNearby = function (query, back) {
  service.request("/zhixuan/api/v1/building/recommendedHouseNearby", "post", query, function (data) {
    back(data.data);
  });
};
//楼盘周边推荐
house_server.recommendedBuildingNearby = function (query, back) {
  service.request("/api/v1/building/recommendedBuildingNearby", "post", query, function (data) {
    back(data.data);
  });
};

/**联合办公品牌及社区******************************************************************************************/
//联合办公品牌基本信息
house_server.getBrandBaseInfo = function (query, back) {
  service.request("/zhixuan/api/v1/community/getBrandBaseInfo", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公品牌页社区印象
house_server.getCommunityImpress = function (query, back) {
  service.request("/zhixuan/api/v1/community/getCommunityImpress", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公品牌热推空间
house_server.getBrandHotPush = function (query, back) {
  service.request("/zhixuan/api/v1/community/getBrandHotPush", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公品牌会员见解
house_server.getBrandOption = function (query, back) {
  service.request("/zhixuan/api/v1/community/getBrandOption", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公社区基本信息
house_server.getCommunityBaseInfo = function (query, back) {
  service.request("/zhixuan/api/v1/community/getCommunityBaseInfo", "post", query, function (data) {
    back(data.data);
  });
};
house_server.getCommunityList = function (query, back) {
  service.request("/zhixuan/api/v1/brand/getBrandDetail", "post", query, function (data) {
    back(data.data);
  });
};
//联合办公社区房源
house_server.getHouseList = function (query, back) {
  service.request("/zhixuan/api/v1/community/getHouseList", "post", query, function (data) {
    back(data.data);
  });
};
//社区附加服务及服务设施列表
house_server.getCommunityPic = function (query, back) {
  service.request("/zhixuan/api/v1/community/getCommunityPic", "post", query, function (data) {
    back(data.data);
  });
};
//推荐社区
house_server.getRecommendCommunity = function (params, back) {
  service.request("/zhixuan/api/v1/filterOrSort/recommendCommunity", "get", params, function (data) {
    back(data.data);
  });
};

/**资产管理公司******************************************************************************************/
//资产管理公司全部房源
house_server.getAllHouse = function (query, back) {
  service.request("/zhixuan/api/v1/company/allHouse", "post", query, function (data) {
    back(data.data);
  });
};
//资产管理公司推荐房源
house_server.getRecommendHouse = function (query, back) {
  service.request("/zhixuan/api/v1/company/recommendHouse", "post", query, function (data) {
    back(data.data);
  });
};
//资产管理公司基本信息
house_server.getCompanyInfo = function (query, back) {
  service.request("/zhixuan/api/v1/company/companyInfo", "post", query, function (data) {
    back(data.data);
  });
};
/**代理公司经纪人店铺******************************************************************************************/
//经纪人店铺基本信息
house_server.getBrokerInfo = function (query, back) {
  service.request("/zhixuan/api/v1/broker/brokerInfo", "post", query, function (data) {
    back(data.data);
  });
};
//经纪人全部房源
house_server.getBrokerAllHouse = function (query, back) {
  service.request("/zhixuan/api/v1/broker/allHouse", "post", query, function (data) {
    back(data.data);
  });
};
//微活动房源列表
house_server.findById = function (query, back) {
  service.request("/zhixuan/api/v1/microActivity/findById", "post", query, function (data) {
    back(data.data);
  });
};
//经纪人店铺推荐房源
house_server.getBrokerRecommendHouse = function (query, back) {
  service.request("/zhixuan/api/v1/broker/recommendHouse", "post", query, function (data) {
    back(data.data);
  });
};
//委托找房录入
house_server.getEntrustCreate = function (query, back) {
  service.request("/zhixuan/api/v1/user/entrust", "post", query, function (data) {
    back(data.data);
  });
};
/**入住平台******************************************************************************************/
//开发商录入
house_server.getDevelopersCreate = function (query, back) {
  service.request("/zhixuan/api/v1/buildingApply/create", "post", query, function (data) {
    back(data.data);
  });
};
//个人业主录入
house_server.getPersonalCreate = function (query, back) {
  service.request("/zhixuan/api/v1/buildingApply/create", "post", query, function (data) {
    back(data.data);
  });
};
//代理公司录入
house_server.getCompanyCreate = function (query, back) {
  service.request("/zhixuan/api/v1/buildingApply/create", "post", query, function (data) {
    back(data.data);
  });
};

/**搜索列表*****************************************************************************************/

house_server.getSearchBuildingHousePage = function (query, back) {
  service.request("/zhixuan/api/v1/building/searchBuildingHousePage", "post", query, function (data) {
    back(data.data);
  });
};
/**搜索列表*****************************************************************************************/
var other_server = new Object();

//关注楼盘或社区
other_server.addAttention = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/addAttention", "post", query, function (data) {
    back(data.data);
  });
};
// 获取用户是否关注此楼盘或社区
other_server.attention = function (query, back) {
  service.request("/zhixuan/api/v1/user/opt/checkAttention", "post", query, function (data) {
    back(data.data);
  });
};